/*
 * XStatusForm.java
 *
 * Created on 21 Январь 2011 г., 10:18
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package chatty.forms;

// #sijapp cond.if modules_XSTATUSES is "true" #
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import chatty.Chatty;
import chatty.comm.*;
import chatty.io.Storage;
import chatty.ui.*;
import chatty.ui.menu.*;
import chatty.modules.*;
import protocol.*;
import protocol.simp.*;
/**
 *
 * @author Vladimir Kryukov
 */
public class XStatusForm extends SomeStatusForm implements TextBoxListener {
    private XStatus status = new XStatus((byte)0, "");
    private InputTextBox message = new InputTextBox("status_message", 256);
    
    public XStatusForm(Simp protocol) {
        super(protocol, SomeStatusForm.SF_STATUS_LIST);
        message.setTextBoxListener(this);
    }
    private static String[] xst_titles  = new String[XStatus.getXStatusCount()];
    static {
        try {
            Storage storage = new Storage("chatty-module-xstatus");
            storage.open(false);
            storage.loadXStatuses(xst_titles/*, xst_descs*/);
            storage.close();
        } catch (Exception e) {
        }
    }
    private void setXStatus(int statusIndex, String title) {
	        if (0 <= statusIndex) {
            xst_titles[statusIndex] = StringConvertor.notNull(title);
           //xst_descs[id]  = StringConvertor.notNull(desc);
            try {
                Storage storage = new Storage("chatty-module-xstatus");
                storage.open(true);
                storage.saveXStatuses(xst_titles/*, xst_descs*/);
                storage.close();
            } catch (Exception e) {
            }
        }
        protocol.getProfile().xstatusIndex = (byte)statusIndex;
        protocol.getProfile().xstatusTitle = title;
        ((Simp)protocol).setXStatus(statusIndex, title);
    }
	
	public void textboxAction(InputTextBox box, boolean ok) {
        if ((message == box) && ok) {
            setXStatus(statusIndex, message.getString());
            back();
        }
    }
    

    protected void statusSelected(int statusIndex) {
        if (-1 == statusIndex) {
    	    setXStatus(statusIndex, "");
    	    back();
            return;
        }
        message.setString(protocol.getProfile().xstatusTitle);
        message.show();
    }

    protected void addStatuses(MenuModel menu) {
		for (int i = 0; i < XStatus.getXStatusCount(); ++i) {
            status.setStatusIndex(i);
            menu.addItem(status.getName(), status.getIcon(), i);
        }
        menu.setDefaultItemCode(protocol.getProfile().xstatusIndex);
    }
}
// #sijapp cond.end #
